{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from requests_html import HTMLSession \n",
    "session = HTMLSession()\n",
    "\n",
    "r = session.get(\"https://prettyprinted.com/\")  #请求抓取链接\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'/#about',\n",
       " '/#courses',\n",
       " '/#tutorials',\n",
       " '/coaching',\n",
       " 'https://courses.prettyprinted.com/courses/enrolled',\n",
       " 'https://courses.prettyprinted.com/p/create-apis-with-django',\n",
       " 'https://courses.prettyprinted.com/p/django-database-essentials',\n",
       " 'https://courses.prettyprinted.com/p/flask-for-beginners',\n",
       " 'https://courses.prettyprinted.com/p/flask-sqlalchemy-basics',\n",
       " 'https://courses.prettyprinted.com/p/intro-to-django-course',\n",
       " 'https://courses.prettyprinted.com/p/intro-to-flask',\n",
       " 'https://courses.prettyprinted.com/p/python-requests',\n",
       " 'https://courses.prettyprinted.com/p/the-flask-extensions-course',\n",
       " 'https://courses.prettyprinted.com/p/understanding-django',\n",
       " 'https://github.com/prettyprinted',\n",
       " 'https://prettyprinted.com',\n",
       " 'https://prettyprinted.com/tutorials/automatically_load_environment_variables_in_flask',\n",
       " 'https://prettyprinted.com/tutorials/creating-a-poll-app-in-django',\n",
       " 'https://twitter.com/pretty_printed',\n",
       " 'https://youtube.com/c/prettyprintedtutorials'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.html.links  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'https://courses.prettyprinted.com/courses/enrolled',\n",
       " 'https://courses.prettyprinted.com/p/create-apis-with-django',\n",
       " 'https://courses.prettyprinted.com/p/django-database-essentials',\n",
       " 'https://courses.prettyprinted.com/p/flask-for-beginners',\n",
       " 'https://courses.prettyprinted.com/p/flask-sqlalchemy-basics',\n",
       " 'https://courses.prettyprinted.com/p/intro-to-django-course',\n",
       " 'https://courses.prettyprinted.com/p/intro-to-flask',\n",
       " 'https://courses.prettyprinted.com/p/python-requests',\n",
       " 'https://courses.prettyprinted.com/p/the-flask-extensions-course',\n",
       " 'https://courses.prettyprinted.com/p/understanding-django',\n",
       " 'https://github.com/prettyprinted',\n",
       " 'https://prettyprinted.com',\n",
       " 'https://prettyprinted.com/#about',\n",
       " 'https://prettyprinted.com/#courses',\n",
       " 'https://prettyprinted.com/#tutorials',\n",
       " 'https://prettyprinted.com/coaching',\n",
       " 'https://prettyprinted.com/tutorials/automatically_load_environment_variables_in_flask',\n",
       " 'https://prettyprinted.com/tutorials/creating-a-poll-app-in-django',\n",
       " 'https://twitter.com/pretty_printed',\n",
       " 'https://youtube.com/c/prettyprintedtutorials'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.html.absolute_links    #抽取绝对链接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "r.html.find(\".title\",first=True) #使用CSS selector "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "title=r.html.find(\"title\",first=True) #将其分配给变量title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Element 'title' >"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Pretty Printed'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title.text   #查看该元素的文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<title>Pretty Printed</title>'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title.html   #查看该元素的HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "subtitle = r.html.find('.subtitle')   # 选择副标题，run没有结果?????\n",
    "subtitle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.session.get('https://courses.prettyprinted.com/p/the-flask-extensions-course')\n",
    "r  # 利用.get(URL)进入prttyprint-FLASK课程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.html.find('.course-section')  #显示Flask课程内容?????"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "r.html.find('.course-section', first=True)  #显示第一个，无结果？？？？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
